<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>

<head>
    <meta charset="UTF-8">
    <title>终端升级</title>
    <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}"/>
    <link rel="stylesheet" th:href="@{/lib/flatpickr/flatpickr.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/base-v2.css}">
    <link rel="stylesheet" th:href="@{/lib/font-awesome/4.7.0/css/font-awesome.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/app.css}">
</head>

<body onload="onLoad(1)">

<div class="app-page">
    <div class="app-page-header">
        <div class="page-title">
            <div class="title">终端升级</div>
        </div>
    </div>

    <div class="app-page-content">
        <div class="app-card">
            <div class="app-card-body">
                <input type="hidden" id="deviceInfoId" name="deviceInfoId">
                <table class="z-table">
                    <thead>
                    <th>ID</th>
                    <th>版本号</th>
                    <th>上传时间</th>
                    <th>操作</th>
                    </thead>
                    <tbody id="deviceUpdateBody">
                    </tbody>
                </table>

                <!-- 分页信息 -->
                <div class="app-card-body">
                    <!-- 前端分页 -->
                    <div class="pagination z-pat z-fr"></div>
                </div>
            </div>
        </div>
    </div>

</div>

<script th:src="@{/lib/jquery.min.js}"></script>
<script th:src="@{/js/popup.js}"></script>
<script th:src="@{/js/admin/app.js}"></script>
<script th:src="@{/js/pagination.js}"></script>
<script type="text/javascript" th:inline="javascript">
    //获取context path
    var context = $("meta[name='ctx']").attr("content");

    var pagination = new Pagination(".pagination", {
        pageIndex: 0, // 当前页数
        pageSize: 0, // 每页数量
        count: 0, // 总条数
        maxButtonCount: 5, // 分页按钮数量(可选)
        // 分页切换事件
        onPageChanged: function(pageIndex) {
            onLoad(pageIndex);
        }
    })

</script>
<script>
    /*$(document).on('click', '.btn-upgrade', function(params) {
        console.log('btn-upgrade');

        // 升级成功
        popup.tipsTop('升级成功', 'success')

        // 升级失败
        // popup.tipsTop('升级失败', 'error')
    })*/


    /**
     * 指定终端升级
     */
    function setDeviceUpdate(deviceUpgradeId) {
        var deviceInfoId = $('#deviceInfoId').val();
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/deviceInfo/setDeviceUpdate',
            data: {deviceInfoId: deviceInfoId, deviceUpgradeId: deviceUpgradeId},
            cache: false,
            async: true,
            success: function (result) {
                alert(result.msg);
                if (result.code == 0) { // 如果操作成功，则刷新当前页面
                    window.location.reload();
                }
            }
        });
    }

    function onLoad(pageIndex) {
        var deviceInfoId = [[${deviceInfoId}]]; // 终端Id
        var deviceModel = '[[${deviceModel}]]'; // 终端机型
        $('#deviceInfoId').val(deviceInfoId);
        //使用ajax获取apk列表
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/deviceUpgrade/apkListforAjax',
            data: {model:deviceModel,pageNum:pageIndex},
            cache: false,
            async: true,
            success: function (result) {
                var page = result.data;
                var pageNum = page.pageNum;
                var pageSize = page.pageSize;
                var total = page.total;
                var array = page.apkList;
                var str = "";
                $.each(array,function(index, n){
                    var deviceUpgradeId = array[index].deviceUpgradeId;
                    str = str+
                        " <tr>\n" +
                        "    <td>"+array[index].deviceUpgradeId+"</td>\n" +   //id
                        "    <td>"+array[index].versionCode+"</td>\n" + //版本号
                        "    <td>"+array[index].updateTime+"</td>\n" + //上传时间
                        "    <td class=\"btn-group\">\n" + //操作
                        "       <a class=\"btn-upgrade\" onclick='setDeviceUpdate("+deviceUpgradeId+")'>升级</a>\n" +
                        "    </td>\n" +
                        " </tr>"
                });
                $('#deviceUpdateBody').html(str);

                pagination.options.count = total;
                pagination.options.pageSize = pageSize;
                pagination.options.pageIndex = pageNum;
                pagination.render();
            }
        });
    }

</script>

</body>

</html>